﻿// Action paths
var ACTION_ACCOUNT = "account/";
var ACTION_READ = "read/";
var ACTION_SEARCH = "search/";
var ACTION_UPLOAD = "upload/";
var ACTION_WRITE = "write/";

// Field names
var FIELD_ERROR = "error";
var FIELD_PAGE = "page";
var FIELD_SUCCESS = "result";
var FIELD_TITLE = "title";

// Input field names
var INPUT_ADMIN = "admin";
var INPUT_ARGUMENT = "argument";
var INPUT_ARTICLE_ID = "articleId";
var INPUT_COMMENT = "commentText";
var INPUT_COMMENTS_ENABLED = "commentsEnabled";
var INPUT_DELETE = "delete";
var INPUT_DELETE_PHOTO = "deletePhoto";
var INPUT_DESCRIPTION = "description";
var INPUT_EMAIL = "email";
var INPUT_ID = "id";
var INPUT_INPUT = "input";
var INPUT_KEYWORDS = "keywords";
var INPUT_NAME = "name";
var INPUT_PAGE = "page";
var INPUT_PASSWORD = "password";
var INPUT_PASSWORD_CONFIRM = "passwordConfirm";
var INPUT_PUBLISH = "publish";
var INPUT_SEARCH = "search";
var INPUT_SIGN_IN_EMAIL = "signInEmail";
var INPUT_SIGN_IN_PASSWORD = "signInPassword";
var INPUT_SUBMIT = "submit";
var INPUT_SUBTITLE = "subtitle";
var INPUT_SUBSCRIBE = "subscribed";
var INPUT_TAG_LIST = "tagList";
var INPUT_TAG_SELECT = "tagSelect";
var INPUT_TEXT = "text";
var INPUT_TITLE = "title";
var INPUT_UPLOAD = "upload";
var INPUT_URL = "url";

// Loading messages
var LOADING = "One sec...";
var UPLOADING = "Uploading, please wait...";

// Server error string
var SERVER_ERROR = "A server error occurred";

// Click source types
var SOURCE_FORM = "form";
var SOURCE_LINK = "link";
var SOURCE_LINKS = "links";
var SOURCE_LIST = "list";
var SOURCE_LOAD = "load";

// Write targets
var TARGET_ARTICLES = "articles";
var TARGET_BUFFER = "buffer";
var TARGET_COMMENTS = "comments";
var TARGET_COMMENT_COUNT = "commentCount";
var TARGET_COMMENT_COUNT_ARTICLE = "commentCountArticle";
var TARGET_COMMENT_FORM = "commentForm";
var TARGET_COMMENT_ERROR = "commentError";
var TARGET_CONTENT = "leftColumn";
var TARGET_FORM_ERROR = "formError";
var TARGET_LINK_ERROR = "linkError";
var TARGET_LINKS = "links";
var TARGET_LIST_ERROR = "listError";
var TARGET_PAGE = "pagination";
var TARGET_SEARCH_ERROR = "searchError";
var TARGET_SUBSCRIBE = "subscribe";
var TARGET_UPLOAD = "uploadedFile";

// Form types
var TYPE_ACTIVATE = "activate";
var TYPE_ARTICLE = "article";
var TYPE_AUTHOR_LIST = "authorList";
var TYPE_COMMENT = "comment";
var TYPE_ENCRYPT = "encrypt";
var TYPE_FORGOT_PASSWORD = "forgotPassword";
var TYPE_PROFILE = "profile";
var TYPE_REGISTER = "register";
var TYPE_SEARCH = "search";
var TYPE_SIGN_IN = "signIn";
var TYPE_SIGN_OUT = "signOut";
var TYPE_SUBSCRIBE = "subscribe";
var TYPE_TAGS = "tags";
var TYPE_UPLOAD = "upload";

// Upload form name and window
var UPLOAD_FILE_LIST = "fileList";
var UPLOAD_FORM = "uploadForm";
var UPLOAD_WINDOW = "uploadWindow";

// Flag to lock forms
var locked = true;

// Response targets
var errorTarget;
var successTarget;

// Flags if sign in status is changing due to request
var signingIn = false;

// Flags if reloading article content
var loadingArticles = false;

// True when target is comment
var targetIsComment = false;

// True when ShareThis is loaded
var sharethisLoaded = false;

// Wait until document is ready
$(document).ready(onLoad);

// Handles page load
function onLoad() {

    // Unlock
    lock(false);

    // Get links or sign in form
    onClick(SOURCE_LOAD, TYPE_SIGN_IN);

    // Clear buffer
    if ($("#" + TARGET_BUFFER)) $("#" + TARGET_BUFFER).html("");

};

// Handles key presses in forms
function onKeyUp(e, type) {

    // Clear all messages
    clearMessages();

    // Submit if enter
    if ((e.keyCode ? e.keyCode : e.which) == 13) onClick(SOURCE_FORM, type);

};

// Handles all sorts of clicks
function onClick(source, type, arg1, arg2) {

    // Declare data and action
    var data = {};
    var action;

    // Must be unlocked to continue
    if (!locked) {

        // Lock forms
        lock(true);

        // Key on type
        switch (type) {

            case TYPE_ACTIVATE:

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_ACTIVATE] = true;

                // Set success target
                successTarget = TARGET_CONTENT;

                // Submitting form
                if (source == SOURCE_FORM) {

                    // Set error target
                    errorTarget = TARGET_FORM_ERROR;

                    // Add form input
                    data[INPUT_EMAIL] = $("#" + INPUT_EMAIL).val();

                } else {

                    // Set error target
                    errorTarget = TARGET_LINK_ERROR;

                };

                break;
            case TYPE_ARTICLE:

                // Triggered by loading
                if (source == SOURCE_LOAD) {

                    // Set action
                    action = ACTION_READ;

                    // Set properties
                    data[INPUT_ARGUMENT] = argument;
                    data[INPUT_PAGE] = page;

                    // Set targets
                    errorTarget = TARGET_ARTICLES;
                    successTarget = TARGET_ARTICLES;

                    // Set loading flag
                    loadingArticles = true;

                } else {

                    // Set action
                    action = ACTION_WRITE;

                    // Set form type
                    data[TYPE_ARTICLE] = true;

                    // Set ID
                    data[INPUT_ID] = arg1;
                    data[INPUT_DELETE] = (arg2 == null) ? false : arg2;

                    // Set success target
                    successTarget = TARGET_CONTENT;

                    // Submitting form
                    if (source == SOURCE_FORM) {

                        // Set error target
                        errorTarget = TARGET_FORM_ERROR;

                        // Build list of selected tags
                        var tags = "";
                        $("#" + INPUT_TAG_SELECT + ":checked").each(function () {
                            tags += ($(this).val()) + ",";
                        });

                        // Add submit value
                        data[INPUT_SUBMIT] = $("#" + INPUT_SUBMIT).val();

                        // Not deleting
                        if ($("#" + INPUT_SUBMIT).val() != "Delete") {

                            // Add form data
                            data[INPUT_ID] = $("#" + INPUT_ID).val();
                            data[INPUT_PUBLISH] = $("#" + INPUT_PUBLISH).is(":checked");
                            data[INPUT_COMMENTS_ENABLED] = $("#" + INPUT_COMMENTS_ENABLED).is(":checked");
                            data[INPUT_TITLE] = $("#" + INPUT_TITLE).val();
                            data[INPUT_SUBTITLE] = $("#" + INPUT_SUBTITLE).val();
                            data[INPUT_DESCRIPTION] = $("#" + INPUT_DESCRIPTION).val();
                            data[INPUT_KEYWORDS] = $("#" + INPUT_KEYWORDS).val();
                            data[INPUT_TAG_SELECT] = tags;
                            data[INPUT_TAG_LIST] = $("#" + INPUT_TAG_LIST).val();
                            data[INPUT_TEXT] = $("#" + INPUT_TEXT).val();

                        };

                    } else {

                        // Set error target
                        errorTarget = TARGET_CONTENT;

                    };

                };

                break;
            case TYPE_AUTHOR_LIST:

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_AUTHOR_LIST] = true;

                // Set response targets
                errorTarget = TARGET_LINK_ERROR;
                successTarget = TARGET_CONTENT;

                break;
            case TYPE_COMMENT:

                // Set action
                action = ACTION_WRITE;

                // Set form type
                data[TYPE_COMMENT] = true;
                if ($("#" + INPUT_ARTICLE_ID)) data[INPUT_ARTICLE_ID] = $("#" + INPUT_ARTICLE_ID).val();

                // Clear form if loading
                if (source == SOURCE_LOAD && $("#" + TARGET_COMMENT_FORM)) $("#" + TARGET_COMMENT_FORM).html("");

                // Set response targets
                errorTarget = TARGET_COMMENT_ERROR;
                successTarget = TARGET_COMMENT_FORM;

                // Submitting form
                if (source == SOURCE_FORM) {

                    // Generic submit set so the script knows we're submitting a form
                    data[INPUT_SUBMIT] = true;

                    // Editing or deleting
                    if (arg1) {

                        // Add ID
                        data[INPUT_ID] = arg1;

                        // Deleting
                        if (arg2) {

                            // Mark deleted
                            data[INPUT_DELETE] = true;

                        } else {

                            // Add update text
                            if ($("#" + arg1 + INPUT_TEXT)) data[INPUT_TEXT] = $("#" + arg1 + INPUT_TEXT).val();

                        };

                        // Change targets
                        successTarget = arg1;
                        errorTarget = arg1 + TARGET_FORM_ERROR;

                    } else {

                        // Set text
                        data[INPUT_TEXT] = $("#" + INPUT_TEXT).val();

                    };

                } else if (source == SOURCE_LINK) {

                    // Iterate through items in buffer
                    $("#" + TARGET_BUFFER).children().each(function () {

                        // This is a comment
                        if ($(this).attr("id").substring(0, 7) == "comment") {

                            // Restore original comment
                            var commentID = $(this).attr("id").substring(7);
                            if ($("#" + commentID)) $("#" + commentID).html($(this).html());

                            // Remove this
                            $(this).remove();

                        };

                    });

                    // Build arguments
                    data[INPUT_ID] = arg1;
                    if (arg2) data[INPUT_DELETE] = true;

                    // Change response targets
                    errorTarget = arg1 + "Error";
                    successTarget = arg1;

                    // Set target is comment flag
                    targetIsComment = true;

                };

                break;
            case TYPE_ENCRYPT:

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_ENCRYPT] = true;

                // Set success target
                successTarget = TARGET_CONTENT;

                // Submitting form
                if (source == SOURCE_FORM) {

                    // Set error target
                    errorTarget = TARGET_FORM_ERROR;

                    // Add form input
                    data[INPUT_INPUT] = $("#" + INPUT_INPUT).val();

                } else {

                    // Set error target
                    errorTarget = TARGET_LINK_ERROR;

                };

                break;
            case TYPE_FORGOT_PASSWORD:

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_FORGOT_PASSWORD] = true;

                // Set success target
                successTarget = TARGET_CONTENT;

                // Submitting form
                if (source == SOURCE_FORM) {

                    // Set error target
                    errorTarget = TARGET_FORM_ERROR;

                    // Add form input
                    data[INPUT_EMAIL] = $("#" + INPUT_EMAIL).val();

                } else {

                    // Set error target
                    errorTarget = TARGET_LINK_ERROR;

                };

                break;
            case TYPE_PROFILE:

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_PROFILE] = true;

                // Set ID
                data[INPUT_ID] = (arg1 == null) ? $("#" + INPUT_ID).val() : arg1;

                // Set success target
                successTarget = TARGET_CONTENT;

                // Key on source
                switch (source) {

                    case SOURCE_LINK:

                        // Set error target
                        errorTarget = TARGET_LINK_ERROR;

                        break;
                    case SOURCE_LIST:

                        // Set error target
                        errorTarget = TARGET_LIST_ERROR;

                        break;
                    case SOURCE_FORM:

                        // Set error target
                        errorTarget = TARGET_FORM_ERROR;

                        // Add form data
                        data[INPUT_ADMIN] = $("#" + INPUT_ADMIN) == null ? false : $("#" + INPUT_ADMIN).is(":checked");
                        data[INPUT_DELETE] = $("#" + INPUT_DELETE) == null ? false : $("#" + INPUT_DELETE).is(":checked");
                        data[INPUT_DELETE_PHOTO] = $("#" + INPUT_DELETE_PHOTO) == null ? false : $("#" + INPUT_DELETE_PHOTO).is(":checked");
                        data[INPUT_EMAIL] = $("#" + INPUT_EMAIL).val();
                        data[INPUT_URL] = $("#" + INPUT_URL).val();
                        data[INPUT_NAME] = $("#" + INPUT_NAME).val();
                        data[INPUT_PASSWORD] = $("#" + INPUT_PASSWORD).val();
                        data[INPUT_PASSWORD_CONFIRM] = $("#" + INPUT_PASSWORD_CONFIRM).val();
                        data[INPUT_SUBMIT] = $("#" + INPUT_SUBMIT).val();

                        break;

                };

                break;
            case TYPE_REGISTER:

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_REGISTER] = true;

                // Set success target
                successTarget = TARGET_CONTENT;

                // Submitting form
                if (source == SOURCE_FORM) {

                    // Set response targets
                    errorTarget = TARGET_FORM_ERROR;

                    // Add form data
                    data[INPUT_EMAIL] = $("#" + INPUT_EMAIL).val();
                    data[INPUT_NAME] = $("#" + INPUT_NAME).val();
                    data[INPUT_PASSWORD] = $("#" + INPUT_PASSWORD).val();
                    data[INPUT_PASSWORD_CONFIRM] = $("#" + INPUT_PASSWORD_CONFIRM).val();
                    data[INPUT_SUBMIT] = $("#" + INPUT_SUBMIT).val();

                } else {

                    // Set response targets
                    errorTarget = TARGET_LINK_ERROR;

                };

                break;
            case TYPE_SEARCH:

                // Set action
                action = ACTION_SEARCH;

                // Set form type
                data[TYPE_SEARCH] = true;

                // Set response targets
                errorTarget = TARGET_SEARCH_ERROR;
                successTarget = TARGET_CONTENT;

                // Add form input
                data[INPUT_SEARCH] = $("#" + INPUT_SEARCH).val();

                break;
            case TYPE_SIGN_IN:

                // Set status
                signingIn = true;

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_SIGN_IN] = true;

                // Set response targets
                errorTarget = TARGET_LINK_ERROR;
                successTarget = TARGET_LINKS;

                // Submitting form
                if (source == SOURCE_FORM) {

                    // Set form data
                    data[INPUT_SIGN_IN_EMAIL] = $("#" + INPUT_SIGN_IN_EMAIL).val();
                    data[INPUT_SIGN_IN_PASSWORD] = $("#" + INPUT_SIGN_IN_PASSWORD).val();

                };

                break;
            case TYPE_SIGN_OUT:

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_SIGN_OUT] = true;

                // Set response targets
                errorTarget = TARGET_LINK_ERROR;
                successTarget = TYPE_SIGN_OUT;

                break;
            case TYPE_SUBSCRIBE:

                // Set action
                action = ACTION_ACCOUNT;

                // Set form type
                data[TYPE_SUBSCRIBE] = true;
                data[INPUT_ARTICLE_ID] = arg1;
                data[INPUT_SUBSCRIBE] = arg2;

                // Set response targets
                errorTarget = TARGET_SUBSCRIBE;
                successTarget = TARGET_SUBSCRIBE;

                break;
            case TYPE_TAGS:

                // Set action
                action = ACTION_WRITE;

                // Set form type
                data[TYPE_TAGS] = true;

                // Set success target
                successTarget = TARGET_CONTENT;

                // Submitting form
                if (source == SOURCE_FORM) {

                    // Set response targets
                    errorTarget = TARGET_FORM_ERROR;

                    // Add form input
                    data[INPUT_TAG_LIST] = $("#" + INPUT_TAG_LIST).val();

                } else {

                    // Set error target
                    errorTarget = TARGET_LINK_ERROR;

                };

                break;

        };

    };

    // Check for elements which are required to continue
    if ($("#" + errorTarget) && data && action) {

        // Copy loading message
        var loading = LOADING;

        // Writing message into content
        if (errorTarget == TARGET_CONTENT) {

            // Move articles to buffer
            $("#" + TARGET_ARTICLES).appendTo("#" + TARGET_BUFFER);
            $("#" + TARGET_CONTENT).html(data[FIELD_SUCCESS]);

            // Update loading message to set style
            loading = "<div id=\"" + TARGET_FORM_ERROR + "\">" + LOADING + "</div>";

        }

        // Writing message into subscribe link
        if (errorTarget == TARGET_SUBSCRIBE) loading = "<span class=\"subscribeError\">" + LOADING + "</span>";

        // Set loading message
        $("#" + errorTarget).html(loading);
        $("#" + errorTarget).css("visibility", "visible");

        // Make request
        $.ajax({
            data: data,
            dataType: "json",
            error: onError,
            success: onSuccess,
            type: "POST",
            url: $("#baseUrl").val() + action
        });

    } else {

        // Unlock if we're missing anything
        lock(false);

    };

};

// Handles successful response from server
function onSuccess(data, textStatus, XMLHttpRequest) {

    // Unlock forms
    lock(false);

    // Clear all messages
    clearMessages();

    // Signing out flag
    var signingOut = false;

    // Error occurred
    if (data[FIELD_ERROR]) {

        // Report error
        $("#" + errorTarget).css("visibility", "visible");
        $("#" + errorTarget).html(data[FIELD_ERROR]);

        // Turn off status flags
        loadingArticles = false;
        signingIn = false;
        targetIsComment = false;

    } else {

        // Deleting
        if (data[FIELD_SUCCESS] == "delete") {

            // Remove targets from document
            if ($("#" + successTarget + "Form")) $("#" + successTarget + "Form").remove();
            if ($("#" + errorTarget)) $("#" + errorTarget).remove();
            if ($("#" + successTarget)) $("#" + successTarget).remove();

        };

        // Target is a comment
        if (targetIsComment) {

            // Clear loading message
            if ($("#" + errorTarget)) $("#" + errorTarget).html("");

            // Copy comment to buffer before setting HTML
            if ($("#" + TARGET_BUFFER) && $("#" + successTarget)) $("#" + TARGET_BUFFER).append("<div id=\"comment" + successTarget + "\">" + $("#" + successTarget).html() + "</div>");

        }

        // Key on target
        switch (successTarget) {

            case TYPE_SIGN_OUT:

                // Clear links
                if ($("#" + TARGET_LINKS)) $("#" + TARGET_LINKS).html("");

                // Check elements
                if ($("#" + TARGET_ARTICLES) && $("#" + TARGET_CONTENT)) {

                    // Restore articles
                    $("#" + TARGET_ARTICLES).appendTo("#" + TARGET_BUFFER);
                    $("#" + TARGET_CONTENT).html("");
                    $("#" + TARGET_ARTICLES).appendTo("#" + TARGET_CONTENT);

                };

                // Set flag
                signingOut = true;

                // Clear target strings
                clearTargets();

                break;
            case TARGET_CONTENT:

                // Move articles to buffer and populate content
                if ($("#" + TARGET_ARTICLES)) $("#" + TARGET_ARTICLES).appendTo("#" + TARGET_BUFFER);
                if ($("#" + TARGET_CONTENT)) $("#" + TARGET_CONTENT).html(data[FIELD_SUCCESS]);

                // Clear target strings
                clearTargets();

                // Set pagination
                if ($("#" + TARGET_PAGE)) $("#" + TARGET_PAGE).html((data[FIELD_PAGE]) ? data[FIELD_PAGE] : "");

                break;
            default:

                // Set target's HTML
                if ($("#" + successTarget)) $("#" + successTarget).html(data[FIELD_SUCCESS]);

                // Clear target strings
                clearTargets();

                // Load comment form
                if (loadingArticles && $("#" + TARGET_COMMENT_FORM) && successTarget != TARGET_COMMENT_FORM) {
                    loadingArticles = false;
                    onClick(SOURCE_LOAD, TYPE_COMMENT);
                }

                // Reload articles when sign in status changes
                if (signingIn && $("#" + TARGET_ARTICLES)) onClick(SOURCE_LOAD, TYPE_ARTICLE);

                // Set pagination
                if ($("#" + TARGET_PAGE)) $("#" + TARGET_PAGE).html((data[FIELD_PAGE]) ? data[FIELD_PAGE] : "");

                break;

        };

    };

    // Turn off flags
    signingIn = false;
    targetIsComment = false;

    // Call sign in if we just signed out to switch from links to form
    if (signingOut) onClick(SOURCE_LOAD, TYPE_SIGN_IN);

    // Set title
    if (data[FIELD_TITLE]) document.title = data[FIELD_TITLE];

    // Update comment count
    var commentCount = $("#comments").children(".comment").length;
    commentCount += (commentCount == "1") ? " comment" : " comments";
    if ($("#" + TARGET_COMMENT_COUNT)) $("#" + TARGET_COMMENT_COUNT).html(commentCount);
    if ($("#" + TARGET_COMMENT_COUNT_ARTICLE)) $("#" + TARGET_COMMENT_COUNT_ARTICLE).html(commentCount);

};

// Handles error
function onError(XMLHttpRequest, textStatus, data) {

    // Turn off status flags
    loadingArticles = false;
    signingIn = false;
    targetIsComment = false;

    // Unlock forms
    lock(false);

    // Clear all messages
    clearMessages();

    // Check element
    if ($("#" + errorTarget)) {

        // Set error message
        $("#" + errorTarget).html(SERVER_ERROR);

        // Set visibility for link and list erros
        if (errorTarget == TARGET_LINK_ERROR || errorTarget == TARGET_LIST_ERROR) $("#" + errortarget).css("visibility", "visible");

    };

    // Clear target strings
    clearTargets();

};

// Upload file
function uploadFile() {

    // Not locked
    if (!locked) {

        // Lock forms
        lock(true);

        // Check upload elements
        if ($("#" + INPUT_UPLOAD) &&
            $("#" + TARGET_UPLOAD) &&
            $("#" + UPLOAD_FORM) &&
            $("#" + UPLOAD_WINDOW)) {

            // Set loading message
            $("#" + TARGET_UPLOAD).html(UPLOADING);

            // Submit form
            $("#" + UPLOAD_FORM).submit();

        };

    };

};

// Handle upload
function onUpload() {

    // Unlock forms
    lock(false);

    // Check upload elements
    if ($("#" + INPUT_UPLOAD) &&
        $("#" + TARGET_UPLOAD) &&
        $("#" + UPLOAD_FORM) &&
        $("#" + UPLOAD_WINDOW)) {

        // Clear file value
        $("#" + INPUT_UPLOAD).val("");

        // Print result from hidden window
        var result = $("#" + UPLOAD_WINDOW).contents().find("#" + UPLOAD_FILE_LIST).html();
        if (result != null) $("#" + TARGET_UPLOAD).html("<div id=\"" + UPLOAD_FILE_LIST + "\">" + result + "</div>");

    };

};

// Clear messages
function clearMessages() {

    // Clear form messages
    if ($("#" + TARGET_FORM_ERROR)) $("#" + TARGET_FORM_ERROR).html("");

    // Search...
    if ($("#searchError")) $("#searchError").html("");

    // List...
    if ($("#" + TARGET_LIST_ERROR)) {

        // Clear error and reset button
        $("#" + TARGET_LIST_ERROR).html("");
        $("#" + TARGET_LIST_ERROR).css("visibility", "hidden");

    };

    // Account links...
    if ($("#" + TARGET_LINK_ERROR)) {

        // Clear error and reset button
        $("#" + TARGET_LINK_ERROR).html("");
        $("#" + TARGET_LINK_ERROR).css("visibility", "hidden");

    };

    // Clear comment message
    if ($("#" + TARGET_COMMENT_ERROR)) $("#" + TARGET_COMMENT_ERROR).html("");

};

// Clear target strings
function clearTargets() {

    // DO IT!
    errorTarget = "";
    successTarget = "";

};

// Set locked status
function lock(status) {

    // Set flag
    locked = status;

    // Locked
    if (locked) {

        // Lock buttons
        if ($("#" + INPUT_SUBMIT)) $("#" + INPUT_SUBMIT).attr("class", "busy");
        if ($("#searchSubmit")) $("#searchSubmit").attr("class", "busy");
        if ($("#signInSubmit")) $("#signInSubmit").attr("class", "busy");

    } else {

        // Unlock buttons
        if ($("#" + INPUT_SUBMIT)) $("#" + INPUT_SUBMIT).attr("class", "ready");
        if ($("#searchSubmit")) $("#searchSubmit").attr("class", "ready");
        if ($("#signInSubmit")) $("#signInSubmit").attr("class", "ready");

    };

};

// Add markup to textarea
function addMarkup(type, target) {

    // DO IT!
    $("#" + target).addMarkup(type);

};